package com.ec.cr.controller;

import com.ec.common.domain.Result;
import com.ec.api.domain.po.MedicineRecord;
import com.ec.cr.mapper.MedicineRecordMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/medicine")
@RequiredArgsConstructor
public class MedicineController {
    private final MedicineRecordMapper medicineRecordMapper;

    /**
     * 根据用户ID和时间范围获取用药记录
     */
    @GetMapping("/records/by-user")
    public Result<List<MedicineRecord>> getMedicineRecordsByUserId(
            @RequestParam("userId") Long userId,
            @RequestParam("startDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam("endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        try {
            List<MedicineRecord> records = medicineRecordMapper.selectRecordsWithDrugName(userId, startDate, endDate);
            return Result.ok(records);
        } catch (Exception e) {
            return Result.error("获取用药记录失败: " + e.getMessage());
        }
    }
}
